﻿/*
 *     获取编码辅助类
 */

using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FileEncoding.Helper
{
    public class EncodingHelper
    {
        private static Dictionary<string, Encoding> _dicEncodings = new Dictionary<string, Encoding>
        {
            {"Big5", Encoding.GetEncoding("BIG5")},
            {"UTF-16BE", Encoding.BigEndianUnicode},
            {"UTF-16LE", Encoding.Unicode},
            {"gb18030", Encoding.GetEncoding("GB2312")},
        };

        public static Encoding GetEncoding(byte[] buffer, string path, out string des)
        {
            des = "Default";
            var charSet = OpenFile(path);
            Encoding encoding = Encoding.Default;

            if(charSet == null)
            {
                if(buffer.Length > 1 && buffer[1] >= 176 && buffer[1] <= 247 && buffer[2] >= 160 && buffer[2] <= 254)
                {
                    encoding = Encoding.GetEncoding("GB2312");
                    des = encoding.BodyName;
                    return encoding;
                }
                else if(buffer.Length <= 1)
                {
                    encoding = IsUTF8OrWithBom(buffer, out des);
                }
                else
                {
                    return null;
                }
            }
            else if (_dicEncodings.TryGetValue(charSet, out encoding))
            {
                des = encoding.BodyName;
            }
            else
            {
                if (charSet == "ASCII" || charSet == "UTF-8")
                {
                    encoding = IsUTF8OrWithBom(buffer, out des);
                }
                else
                {
                    try
                    {
                        encoding = Encoding.GetEncoding(charSet);
                        des = Encoding.GetEncoding(charSet).BodyName;
                    }
                    catch
                    {
                        encoding = null;
                    }
                }
            }

            //switch (charSet)
            //{
            //    case "Big5":
            //        des = Encoding.GetEncoding("BIG5").BodyName;
            //        return Encoding.GetEncoding("BIG5");
            //    case "UTF-16BE":
            //        des = Encoding.BigEndianUnicode.BodyName;
            //        return Encoding.BigEndianUnicode;
            //    case "UTF-16LE":
            //        des = Encoding.Unicode.BodyName;
            //        return Encoding.Unicode;
            //    case "gb18030":
            //        des = "GB2312";
            //        return Encoding.GetEncoding("GB2312");
            //    case "UTF-8":
            //        if (buffer.Length > 1 && buffer[0] == 239 && buffer[1] == 187 && buffer[2] == 191)
            //        {
            //            Console.WriteLine("UTF8");
            //            des = "UTF-8 with BOM";
            //            return Encoding.UTF8;
            //        }
            //        else if (EncodingType.IsUTF8Bytes(buffer))
            //        {
            //            Console.WriteLine("UTF8 no bom is true");
            //            des = "UTF-8";
            //            return new UTF8Encoding(false);
            //        }
            //        break;
            //    case "ASCII":
            //        if (buffer.Length > 1 && buffer[0] == 239 && buffer[1] == 187 && buffer[2] == 191)
            //        {
            //            Console.WriteLine("UTF8");
            //            des = "UTF-8 with BOM";
            //            return Encoding.UTF8;
            //        }
            //        else if (EncodingType.IsUTF8Bytes(buffer))
            //        {
            //            Console.WriteLine("UTF8 no bom is true");
            //            des = "UTF-8";
            //            return new UTF8Encoding(false);
            //        }
            //        break;
            //}

            return encoding;
        }

        private static string OpenFile(string fileName)
        {
            using (FileStream fs = File.OpenRead(fileName))
            {
                Ude.CharsetDetector cdet = new Ude.CharsetDetector();
                cdet.Feed(fs);
                cdet.DataEnd();
                return cdet.Charset;
            }
        }

        private static Encoding IsUTF8OrWithBom(byte[] buffer, out string des)
        {
            if (buffer.Length > 1 && buffer[0] == 239 && buffer[1] == 187 && buffer[2] == 191)
            {
                des = "UTF-8 with BOM";
                return Encoding.UTF8;
            }

            des = "UTF-8";
            return new UTF8Encoding(false);
        }
    }
}
